﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UdpPeer.Protocols;
using UdpPeer.ViewModels;

namespace UdpPeer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        MainWindowModel model = null;
        public MainWindow()
        {
            InitializeComponent();

            model = Resources["MainWindowModelDataSource"] as MainWindowModel;
            model.Dispatcher = Dispatcher;
            // //OK 调试Process xp 可见 System.Threading.Tasks.Task.Factory.StartNew(() => { while (true) { ttt(); } });
            
            //OK 调试Process xp 可见 System.Threading.Tasks.Task.Factory.StartNew(cycle);
            //OK 调试Process xp 可见 System.Threading.Thread thr = new System.Threading.Thread(cycle);
            //OK 调试Process xp 可见 thr.Start();

        }

        void cycle()
        {
            while (true) { ttt(); }
        }

        void ttt() { string sb = "sdfsdfdsfsd"; }

        /// <summary>
        /// 选对事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            model.SelectRowCmd.Execute(e.AddedItems);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var btn = (Button)sender;

            Ents ent = btn.Tag as Ents;
            if (model.DispatcherServer != null && ent != null && ent != model.DispatcherServer)
            {
                var bytes = System.Text.Encoding.UTF8.GetBytes(
                                   MessagePackegHelper.Instance.GetSendProtocolString(new 点对点请求()
                                   {
                                       发起请求节点 = model.当前公网节点信息,
                                       请求目标节点 = ent,
                                   }
                                   ));
                //向调度服务器发送PTOP请求
                model.node.Send(bytes, model.DispatcherServer.节点);
                model.Status = "Pto" + ent.节点;
            }

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            model.PeerNodes.Clear();
        }
    }
}
